<?php
class MyEmailComponent extends Object
{
    public $components = array('Email');
    public $controller;

    public $to = "";
    public $subject = "";
    public $template = "default";
    public $sendAs = "html";
    public $from = "";
    public $data = null;

    protected $modelSmtp= "SmtpSetting";


    public function startup(&$controller)
    {
        $this->controller = $controller;
    }

    public function send()
    {
        $this->Email->to = $this->to;
        $this->Email->subject = $this->subject;
        $this->Email->from = $this->from;
        $this->Email->template = $this->template; // note no '.ctp'

        $conf = $this->controller->{$this->modelSmtp}->get_smtp(array('active' => 1, 'deleted' => 0), 'first');
        if ($conf) {
           /* SMTP Options */
           $this->Email->smtpOptions = array(
                'port' => $conf['SmtpSetting']['outgoing'],
                'timeout' => $conf['SmtpSetting']['timeout'],
                'host' => $conf['SmtpSetting']['host'],
                'username' => $conf['SmtpSetting']['uid'],
                'password' => $conf['SmtpSetting']['pwd'],
           );

            /* Set delivery method */
            $this->Email->delivery = 'smtp';

            //Send as 'html', 'text' or 'both' (default is 'text')
            // because we like to send pretty mail
            $this->Email->sendAs = $this->sendAs;

            //Set view variables as normal
            $this->controller->set('data', $this->data);

            //Do not pass any args to send()
            //return $this->Email->send();
            return array(
                'send'      => $this->Email->send(),
                'smtpError' => $this->Email->smtpError
            );
        } else {
            return null;
        }
    }
}
?>